package com.littlerpc.proxy.proxy;

import com.littlerpc.protocol.net.annotation.RpcProxy;
import com.littlerpc.proxy.util.ProxyUtil;
import lombok.Setter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Set;

/**
 * Created by ChengPeng on 2017/6/13.
 * 这个类目前没有用到，原因是，无法使用在spring中使用注解获取代理对象
 * 如果后续有有需要，可能会在是使用这个，只是先留一个备份
 */

@Deprecated
public class ClientProxyFactory implements ApplicationContextAware, InitializingBean {

    @Setter
    private String[] scanPackages;
    @Setter
    private RemoteClientInfo remoteClientInfo;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("**********before****************");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();

        Object bean = beanFactory.getBean("client");

        //获取所有RpcProxy注解类
        Set<Class<?>> classesWithAnnotationClass = ProxyUtil.getClassesWithAnnotationClass(RpcProxy.class, scanPackages);
        beanFactory.getAutowireCandidateResolver();
        classesWithAnnotationClass.forEach(cla -> {
            registerProxy(beanFactory, cla);
        });


    }

    private void registerProxy(DefaultListableBeanFactory beanFactory, Class<?> cla) {

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ClientProxyFactoryBean.class);

        beanDefinitionBuilder.addPropertyValue("remoteClientInfo", remoteClientInfo);
        beanDefinitionBuilder.addPropertyValue("interfaceClass", cla);
        //获取注解的参数
        String name = cla.getAnnotation(RpcProxy.class).value();
        beanFactory.registerBeanDefinition(name, beanDefinitionBuilder.getRawBeanDefinition());
    }

   /* private FactoryBean createProxy(Class<?> aClass) {
        MethodInterceptor clientProxy = new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

                Object ret = null;

                String name = method.getName();
                if (name.equals("isSingleton")) {
                    ret = true;
                } else if (name.equals("getObjectType")) {
                    return aClass;
                } else if (name.equals("getObject")) {
                    Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{aClass}, new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            RpcClient client = remoteClientInfo.rpcClient();
                            //构建请求对象
                            RpcRequest request = RpcRequest.buildRequest(aClass, method, args);
                            //将请求对象通过RpcClient发送到服务提供端
                            Object send = client.send(request);
                            return send;
                        }
                    });
                }
                return ret;
            }
        };

        Enhancer e = new Enhancer();
        e.setInterfaces(new Class[]{FactoryBean.class});
        e.setCallback(clientProxy);

        return (FactoryBean) e.create();

    }*/

}
